package dao;

import model.Blog;
import utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/08  11:43
 */
public class BlogDao {
    /**
     * 写入博客
     * @param blog
     * @return 返回执行成功的条数
     */
    public int insert(Blog blog){
        if(blog == null){
            return 0;
        }
        Connection connection = null;
        PreparedStatement statement = null;
        int result = 0;

        try {
            connection = DBUtil.getConnection();
            String sql = "insert into blog value(null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setTimestamp(3,blog.getCreateTime());
            statement.setLong(4,blog.getUserId());
            result = statement.executeUpdate();
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null, statement, connection);
        }
    }

    /**
     * 删除博客
     * @param blogId 要删除的Id
     * @return 返回执行成功的条数
     */
    public int delete(Long blogId){
        if(blogId == null){
            return 0;
        }
        Connection connection = null;
        PreparedStatement statement = null;
        int result = 0;

        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setLong(1,blogId);
            result = statement.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(null, statement, connection);
        }
        return result;
    }

    /**
     * 按Id查询博客详情页
     * @param blogId 博客Id
     * @return 返回查询的博客内容
     */
    public Blog selectById(Long blogId){
        if(blogId == null){
            return null;
        }
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //获取连接
            connection = DBUtil.getConnection();
            //构造sql
            String sql = "select id,title,content,createTime,userId from blog where id = ?";
            //获取预处理对象
            statement = connection.prepareStatement(sql);
            //替换占位符
            statement.setLong(1,blogId);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getLong("id"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setCreateTime(resultSet.getTimestamp("createTime"));
                blog.setUserId(resultSet.getLong("userId"));
                return blog;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return null;
    }

    /**
     * 查询所有的博客
     * @return
     */
    public List<Blog> selectAll(){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> result = null;
        try {
            //获取连接
            connection = DBUtil.getConnection();
            //构造sql
            String sql = "select id,title,content,createTime,userId from blog order by id desc";
            //获取预处理对象
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                if(result == null){
                    result = new ArrayList<>();
                }
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getLong("id"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setCreateTime(resultSet.getTimestamp("createTime"));
                blog.setUserId(resultSet.getLong("userId"));
                result.add(blog);
            }
            return result;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(resultSet, statement, connection);
        }
    }
}
